//
//  ConsultViewController.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/12.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ConsultViewController.h"
#import "BaseCollectionView.h"
#import "MJMacro.h"
#import "CVCellOfConsult.h"
#import "CVCellOfSpecialistCharts.h"
@interface ConsultViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) BaseCollectionView *collectionViewOfConsult;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@end

@implementation ConsultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"话题榜", @"专家榜"]];
    self.segmentedControl.selectedSegmentIndex = 0;
    [self.segmentedControl addTarget:self action:@selector(clickSegment:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = _segmentedControl;
    // 创建子控件
    [self createSubView];
}
#pragma mark - segmented 的点击方法
- (void)clickSegment:(UISegmentedControl *)segmentControl{
    
    switch (segmentControl.selectedSegmentIndex) {
        case 0:
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.1];
            self.collectionViewOfConsult.contentOffset = CGPointMake(0, 0);
            [UIView commitAnimations];
        }
            break;
        default:
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.1];
            self.collectionViewOfConsult.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
               [UIView commitAnimations];
        }
            break;
    }
}
#pragma mark - scrolleView 的滑动方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == SCREEN_WIDTH) {
        _segmentedControl.selectedSegmentIndex = 1;
        [self clickSegment:_segmentedControl];
    } else {
        _segmentedControl.selectedSegmentIndex = 0;
        [self clickSegment:_segmentedControl];
    }
}
#pragma mark - 创建子控件
- (void)createSubView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionViewOfConsult = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
    self.collectionViewOfConsult.backgroundColor = [UIColor whiteColor];
    self.collectionViewOfConsult.pagingEnabled = YES;
    [self.view addSubview:_collectionViewOfConsult];
    self.collectionViewOfConsult.delegate = self;
    self.collectionViewOfConsult.dataSource = self;
    [self.collectionViewOfConsult registerClass:[CVCellOfConsult class] forCellWithReuseIdentifier:@"CVCellOfConsult"];
    [self.collectionViewOfConsult registerClass:[CVCellOfSpecialistCharts class] forCellWithReuseIdentifier:@"CVCellOfSpecialistCharts"];
}
#pragma mark - collectionView 代理方法 row 数量的确定
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}
#pragma mark - CollectionView 代理方法 cell 的赋值
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        CVCellOfConsult *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfConsult" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor orangeColor];
        return cell;
    } else {
        CVCellOfSpecialistCharts *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CVCellOfSpecialistCharts" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor greenColor];
        return cell;
    }
}
#pragma mark - 其他, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
